package com.aigame.socket;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.aigame.entity.User;
import com.aigame.util.EncryptionTools;
import com.aigame.util.NetTransferUtil;

public class UserSocket extends BaseSocket {
	public UserSocket() {
	}
	public UserSocket(String appid, String partner) {
		super(appid, partner);
	}
	public User login(User user) throws Exception {
		String url = "http://api.aigame365.com/api/loginService.shtml";
		String encryPass = EncryptionTools.md5(user.getPassword() + PRIVATE_KEY);
		Log.i("user info", user.getUsername()+"---"+user.getPassword());
		paramsMap.put("username", user.getUsername());
		paramsMap.put("password", encryPass);
		paramsMap.put("appid", appid);
		paramsMap.put("partner", partner);
		String macString = EncryptionTools.md5(appid + user.getUsername() + encryPass
				+ partner + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,10,10);
		Log.i("login result", resultString);
		if(resultString==null) {
			throw new Exception("network timeout");
		}
		JSONObject resultObj = null;
		try {
			resultObj = new JSONObject(resultString);
			String result = (String) resultObj.get("result");
			JSONObject userInfo = (JSONObject)resultObj.get("info");
			if (result.equals("1")) {
				user.setUid(userInfo.getString("uid"));
				user.setSid(userInfo.getString("sid"));
				return user;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	public Boolean register(User user) throws Exception {
		String url = "http://api.aigame365.com/api/regService.shtml";
		String encryPass = EncryptionTools.md5(user.getPassword() + PRIVATE_KEY);
		paramsMap.put("username", user.getUsername());
		paramsMap.put("password", encryPass);
		paramsMap.put("appid", appid);
		paramsMap.put("partner", partner);
		String macString = EncryptionTools.md5(appid + user.getUsername() + encryPass
				+ partner + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,10,10);
		JSONObject resultObj = null;
		if(resultString==null) {
			throw new Exception("network timeout");
		}
		try {
			resultObj = new JSONObject(resultString);
			String result = (String) resultObj.get("result");
			if (result.equals("1")) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}
	public String getServerRegUsername() throws Exception{
		String url = "http://api.aigame365.com/api/getusername.shtml";
		paramsMap.put("appid", appid);
		String macString = EncryptionTools.md5(appid + PRIVATE_KEY);
		paramsMap.put("mac", macString);
		String resultString = new NetTransferUtil().post(url, paramsMap,2,2);
		if(resultString==null) {
			throw new Exception("network timeout");
		}
		Log.i("debug_print", resultString);
		JSONObject resultObj = null;
		try {
			resultObj = new JSONObject(resultString);
			String username = (String) resultObj.get("username");
			if (username != null&&!username.equals("")) {
				return username;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
